package com.wngbms.netty.seatdemo.service;

import com.alibaba.fastjson.JSON;
import com.wngbms.netty.seatdemo.bean.ExtraEventInfo;
import com.wngbms.netty.seatdemo.comm.*;
import com.wngbms.netty.seatdemo.message.BaseHeader;
import com.wngbms.netty.seatdemo.message.SeatOutMessage;
import com.wngbms.netty.seatdemo.server.SeatBizHandler;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

@Slf4j
public class ExtraInfoHandler implements SeatTypeHandler {

    public static final CmdType CMD_TYPE = CmdType.EXTRA_INFO;

    @Override
    public void handleOutbound(String userId) {
        log.info("推送事件额外信息:{}", userId);

        ExtraEventInfo extra = findExtra();
        byte[] bytes = JSON.toJSONString(extra).getBytes(StandardCharsets.UTF_8);
        BaseHeader baseHeader = BaseHeader.build(MagicVersion.V1, CmdType.EXTRA_INFO,
                BodyType.JSON, EncryptionType.NON, ReqOrResp.REQ, bytes.length);

        Channel channel = SeatBizHandler.USER_CHANNEL_MAP.get(userId);
        SeatOutMessage seatOutMessage = new SeatOutMessage();
        seatOutMessage.setBaseHeader(baseHeader);
        seatOutMessage.setBody(bytes);
        channel.writeAndFlush(seatOutMessage);
    }

    public ExtraEventInfo findExtra() {
        ExtraEventInfo extraEventInfo = new ExtraEventInfo();
        extraEventInfo.setVisitorName("吴亦凡");
        extraEventInfo.setVisitorTel("1324563211");
        return extraEventInfo;
    }
}
